
/* 
   moduleName : 用户表单检测构造函数 
   createDate : 2018年4月9日
   description : 用于用户表单的前端验证。
*/
function UserInfoEditor() {
    var baseUrl = "/m";
    var saveButton = $("#btn_save_user_Info");
    saveButton.click(this.inputUserInfo);

    this.setSaveButtonText = function (text) {
        saveButton.html(text);
    }

    this.showUserInfoInput = function () {
        $("#myModalUserInfo").modal('show');
    }

    this.inputUserInfo = function () {
        saveButton.attr('disabled', true);

        var schoolName = $("#list_school_name").val();
        var qq = $("#list_qq").val();
        var valid = true;

        if (!checkInput(schoolName, qq)) {
            //输入信息无效
            saveButton.removeAttr('disabled');
            valid = false;
        }
        //var area = $("#list_area").val();
        var area = "";
        var oldProvince = $("#oldProvince").val();
        var oldCity = $("#oldCity").val();
        var oldDistrict = $("#oldDistrict").val();
        if ($("span [data-count='province']") != null && $("span [data-count='province']") != undefined) {

            area += $("span [data-count='province']").text();
        }
        if ($("span [data-count='city']") != null && $("span [data-count='city']") != undefined) {
            if (area != "") {
                area += "-";
            }

            area += $("span [data-count='city']").text();
        }
        if ($("span [data-count='district']") != null && $("span [data-count='district']") != undefined) {
            if (area != "") {
                area += "-";
            }

            area += $("span [data-count='district']").text();
        }
        if (area.trim() == "" || area == "请选择地区" || (area.split("-").length < 3) || (area.split("-")[0].length <= 0) || (area.split("-")[1].length <= 0) || (area.split("-")[2].length <= 0)) {
            $("#inputError2").css({ 'visibility': 'visible' });
            saveButton.removeAttr('disabled');
            valid = false;
        }

        var phone = $("#list_phone").val();
        if (phone.trim() == "" || !(/^1[34578]\d{9}$/.test(phone))) {
            $("#inputError3").css({ 'visibility': 'visible' });
            saveButton.removeAttr('disabled');
            valid = false;
        }



        var schoolType = $("#schoolType").val();
        if (schoolType == "-1") {
            $("#inputError6").css({ 'visibility': 'visible' });
            saveButton.removeAttr('disabled');
            valid = false;
        }
        var jobType = $("#list_job").val();
        if (jobType == "-1") {
            $("#inputError7").css({ 'visibility': 'visible' });
            saveButton.removeAttr('disabled');
            valid = false;
        }
        var ageArrange = $("#list_age").val();
        if (ageArrange == "-1") {
            $("#inputError8").css({ 'visibility': 'visible' });
            saveButton.removeAttr('disabled');
            valid = false;
        }
        var email = $("#list_mail").val();
        var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if ((email != null && email != "" && email != undefined) && !myreg.test(email)) {
            $("#inputError5").css({ 'visibility': 'visible' });
            saveButton.removeAttr('disabled');
            valid = false;
        }

        if (!valid) return;
        jQuery.ajax({
            type: "POST",
            url: baseUrl + "/userInfo/updateUserInfoV1",
            data: { "schoolName": schoolName, "qqNumber": qq, "area": area, "phone": phone, "schoolType": schoolType, "jobType": jobType, "ageArrange": ageArrange, "email": email },
            dataType: "json",
            cache: false,
            success: function (ret) {
                //用户数据更新成功
                saveButton.removeAttr('disabled'); //其实不需要，为了双保险
                if (ret == 1 || ret == "1") { //任意一个都是对的，短路特性不影响效率,还尼玛短路特性都出来了。。就是一个或的条件
                    //显示数据更新成功
                }
                location.reload();
            }
        });
    }

    /**
     * 检查用户输入是否有效
     * @param schoolName
     * @param qq
     * @returns {boolean}
     */
    function checkInput(schoolName, qq) {
        // QQ 验证正则表达式。
        var patternQQ = /^[1-9][0-9]{5,12}$/;
        var valid = true;

        //如果在遇到错就返回，不能保证在之前有两空错的情况即时再次检查
        if (schoolName.trim() == "") {
            $("#inputError1").css({ 'visibility': 'visible' });
            valid = false;
        } else {
            $("#inputError1").css({ 'visibility': 'hidden' });
        }

        if (qq != "" && !patternQQ.test(qq)) {
            $("#inputError4").css({ 'visibility': 'visible' });
            valid = false;
        } else {
            $("#inputError4").css({ 'visibility': 'hidden' });
        }

        /*if (valid) {
         $("#inputError1").remove();
         $("#inputError4").remove();
         }*/
        return valid;
    }
}



var AddressCom_city = function (suffix) {
    suffix = (suffix != undefined ? '_' + suffix : '');
    var address_level_info = [],
        address_level_codes = [];
    //address_level_codes=["360000", "360700", "360726"];
    //address_level_info =["广东", "广州", "天河区"];
    var rand_id = (Math.random() * (999999 - 100000) + 100000).toString().split('\.')[0];
    var onchange = function (data) { },
        is_user_click = true;

    function eare() {
        $.ajax({
            type: "GET",
            url: "http://www.lpaiche.com/Index/address?level=0",
            dataType: "jsonp",
            success: function (res) {
                $.each(res.data, function (i, item) {
                    if (item.city_code != "710000" &&
                        item.city_code != "810000" &&
                        item.city_code != "820000") {
                        $(".arer-sheng_" + rand_id + " ul").append("<li code='" + item.city_code + "'>" + item.city_name + "</li>");
                    }
                })

                sheng_sel();

                $.each(res.data, function (i, item) {
                    if (item.city_code == address_level_codes[0]) {
                        is_user_click = false;
                        $(".arer-sheng_" + rand_id + " ul li").eq(i).click();
                        is_user_click = true;
                        return;
                    }
                })

                $.each(res.data, function (i, item) {
                    if (item.city_name == address_level_info[0]) {
                        is_user_click = false;
                        $(".arer-sheng_" + rand_id + " ul li").eq(i).click();
                        is_user_click = true;
                        return;
                    }
                })
            }
        })

        function sheng_sel() {
            $(".arer-sheng_" + rand_id + " ul li").each(function () {
                $(this).click(function (event) {
                    if (is_user_click) {
                        address_level_info = [];
                        address_level_codes = [];
                    }
                    address_level_info[0] = $(this).html();
                    address_level_codes[0] = $(this).attr('code');
                    onchange(getvals());
                    var province = $(this).html();
                    event.stopPropagation();
                    var li_id = $(this).attr("code");
                    $(this).parent().parent().hide();
                    $(".arer-shi_" + rand_id).show();
                    $(".arer-shi_" + rand_id + " ul").html("");
                    $.ajax({
                        type: "GET",
                        url: "http://www.lpaiche.com/Index/address?pid=" + li_id,
                        cache: true,
                        dataType: "jsonp",
                        success: function (res) {
                            if (res.data != null) {
                                $.each(res.data, function (i, item) {
                                    $(".arer-shi_" + rand_id + " ul").append("<li code='" + item.city_code + "'>" + item.city_name + "</li>");
                                });
                                shi_sel();
                                if ($(".arer-shi_" + rand_id + " ul li").length == 1) {
                                    $(".arer-shi_" + rand_id + " ul li").click();
                                    $(".arer-shi_" + rand_id).hide();
                                    return;
                                }
                                $.each(res.data, function (i, item) {
                                    if (item.city_code == address_level_codes[1]) {
                                        $(".arer-shi_" + rand_id + " ul li").eq(i).click();
                                        return;
                                    }
                                });
                                $.each(res.data, function (i, item) {
                                    if (item.city_name == address_level_info[1]) {
                                        $(".arer-shi_" + rand_id + " ul li").eq(i).click();
                                        return;
                                    }
                                });
                            }
                        }
                    });
                });
            });
        }

        function shi_sel() {
            $(".arer-shi_" + rand_id + " ul li").each(function () {
                var city = this;
                $(city).click(function (event) {
                    address_level_info[1] = $(this).html();
                    address_level_codes[1] = $(this).attr('code');
                    onchange(getvals());
                    //console.log($(city).html());
                    event.stopPropagation();
                    var li_id = $(this).attr("code");
                    $(this).parent().parent().hide();
                    $(".arer-qu_" + rand_id + "").show();
                    $(".arer-qu_" + rand_id + " ul").html("");
                    var zhixiashi = ['北京', '上海', '天津', '重庆'];
                    $(".city .area_" + rand_id + " input").val(($.inArray(address_level_info[0], zhixiashi) < 0 ? address_level_info[0] + "-" : '') + address_level_info[1]);
                    $.ajax({
                        type: "GET",
                        url: "http://www.lpaiche.com/Index/address?pid=" + li_id,
                        cache: true,
                        dataType: "jsonp",
                        success: function (res) {
                            $.each(res.data, function (i, item) {
                                $(".arer-qu_" + rand_id + " ul").append("<li code='" + item.city_code + "'>" + item.city_name + "</li>");
                            });
                            qu_sel();
                            $.each(res.data, function (i, item) {
                                if (item.city_code == address_level_codes[2]) {
                                    $(".arer-qu_" + rand_id + " ul li").eq(i).click();
                                    return;
                                }
                            });
                            $.each(res.data, function (i, item) {
                                if (item.city_name == address_level_info[2]) {
                                    $(".arer-qu_" + rand_id + " ul li").eq(i).click();
                                    return;
                                }
                            });
                        }
                    })
                })
            })
        }

        function qu_sel() {
            $(".arer-qu_" + rand_id + " ul li").click(function () {
                address_level_info[2] = $(this).html()
                address_level_codes[2] = $(this).attr('code');
                onchange(getvals());
                $(this).parent().parent().hide();
                var zhixiashi = ['北京', '上海', '天津', '重庆'];
                $(".area_" + rand_id + " input").val(($.inArray(address_level_info[0], zhixiashi) < 0 ? address_level_info[0] + "-" : '') + address_level_info[1] + "-" + address_level_info[2]);

                var vals = [],
                    data = getvals();
                $.each(data, function (i, n) {
                    vals.push(i);
                });
                var adds = [$('#province_' + rand_id + ''), $('#city_' + rand_id + ''), $('#area_' + rand_id + '')];
                $.each(adds, function (i, n) {
                    adds[i].val(vals[i]);
                })

            })
        }
    }

    $(function () {
        $(".area_" + rand_id + " input").click(function (event) {
            $(".arer-sheng_" + rand_id).show();
            $(".arer-shi_" + rand_id).hide();
            $(".arer-qu_" + rand_id).hide();
            event.stopPropagation();
        });
        $("body").click(function () {
            $(".arer-sheng_" + rand_id).hide();
            $(".arer-shi_" + rand_id).hide();
            $(".arer-qu_" + rand_id + "").hide();
        });
        eare();
    });

    function getvals() {
        var ret = {};
        for (var i in address_level_codes) {
            ret[address_level_codes[i]] = address_level_info[i];
        }
        return ret;
    };

    function getarr() {
        var ret = [];
        for (var i in address_level_codes) {
            ret.push({
                code: address_level_codes[i],
                name: address_level_info[i]
            });
        }
        return ret;
    };
    this.getarr = getarr;
    this.getvals = getvals;
    this.set_init = function (init_vals) {
        if (typeof (init_vals[0]) == undefined)
            return;

        if (parseInt(init_vals[0]) > 0) {
            address_level_codes = init_vals;
        } else {
            address_level_info = init_vals;
        }

    };
    this.set_change = function (change) {
        onchange = change;
    }

    function init_html() {
        var html = '<input type="hidden" id="province_' + rand_id + '"  name="province' + suffix + '" value="" />' +
            '<input type="hidden" id="city_' + rand_id + '"  name="city' + suffix + '" value="" />' +
            '<input type="hidden" id="area_' + rand_id + '"  name="area' + suffix + '" value="" />'
        html += '<div class="' + 'area_' + rand_id + ' area">' +
            '<input type="text" value="请选择地区" readonly style="width: 100%;height:35px" class="form-control"/>' +
            '<div class="arer-sheng_' + rand_id + ' arer-sheng"><ul></ul></div>' +
            '<div class="arer-shi_' + rand_id + ' arer-shi" ><ul></ul></div>' +
            '<div class="arer-qu_' + rand_id + ' arer-qu"><ul></ul></div></div>';
        return html;
    };
    document.write(init_html());
};


